layui.use(['table', 'admin', 'ax', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
     * 职位表管理
     */
    var Position = {
        tableId: "positionTable"
    };

    /**
     * 初始化表格的列
     */
    Position.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', sort: true, title: '主键id'},
            {field: 'run', sort: true, title: '轮次', width: '5%'},
            {field: 'date', sort: true, title: '比赛时间'},
            {field: 'teamAName', title: '主队', templet: '#teamATpl'},
            {field: 'score', title: '比分(半场)'},
            {field: 'teamBName', title: '客队', templet: '#teamBTpl'},
            {field: 'shots', title: '角球', templet: '#cornersTpl'},
            {field: 'shots', title: '射正', templet: '#shotsTpl'},
            {field: 'faults', title: '犯规', templet: '#faultsTpl'},
            {field: 'minuteGoal', title: '1-15分钟进球数'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Position.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val().trim();
        queryData['fkLeagueId'] = $("#fkLeagueId").val();
        queryData['ownGoal'] = $("#ownGoal").val();
        queryData['pointGoal'] = $("#pointGoal").val();
        queryData['redCard'] = $("#redCard").val();
        queryData['year'] = $("#year").val();
        table.reload(Position.tableId, {
            where: queryData, page: {curr: 1},
            done: function () {
                let team = $(".team");
                for (var i = 0; i < team.length; i++) {
                    if ($(team[i]).text() == $("#name").val()) {
                        $(team[i]).css({background: "yellow"});
                    }
                }
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Position.exportExcel = function () {
        var checkRows = table.checkStatus(Position.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    var first = true;

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Position.tableId,
        url: Feng.ctxPath + '/info/gameList',
        where: {
            'fkLeagueId': $("#fkLeagueId").val(),
            'year': $("#year").val(),
        },
        // curr: location.hash.replace('#!fenye=', $("#currentRun").val()), //获取起始页
        curr: 2, //获取起始页
        groups: 10,
        page: true,
        limit: $("#teamNum").val(),
        height: "full-158",
        cellMinWidth: 100,
        limits: $("#nums").val().toString().split(","),
        even: true,
        cols: Position.initColumn(),
        //在表格加载完毕后执行的方法
        done: function (res, curr, count) {
            if (curr != $("#currentRun").val() && first) {
                $(".layui-laypage-skip").find("input").val($("#currentRun").val());
                $(".layui-laypage-btn").click();
                first = false;
            }
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Position.search();
    });

    // 添加按钮点击事件
    $('#btnView').click(function () {
        layer.alert($("#condition").val())
    });

    /**
     * 弹出添加对话框
     */
    Position.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/info/view';
    };

    // 导出excel
    $('#btnCancel').click(function () {
        console.log("12123")
        $("#name").val("");
        $("#ownGoal").val("");
        $("#pointGoal").val("");
        $("#redCard").val("");
        $("#year").val("");
        form.render();
    });

    // 工具条点击事件
    table.on('tool(' + Position.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Position.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Position.onDeleteItem(data);
        }
    });

    // 修改user状态
    form.on('switch(status)', function (obj) {
        var positionId = obj.elem.value;
        var checked = obj.elem.checked ? true : false;
        Position.changeStatus(positionId, checked);
    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + '/info'
    });
});
